package cn.ibizlab.util.helper;

import cn.ibizlab.util.domain.IEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;

public class CachedBeanCopier {

    private CachedBeanCopier() {
        throw new IllegalStateException("Utility class");
    }

    public static void copy(Object srcObj, Object destObj) {
        copy(srcObj, destObj, true);
    }

    public static void copy(Object srcObj, Object destObj, boolean bIncEmpty) {
        if (srcObj == null || destObj == null) {
            return;
        }
        else if(srcObj instanceof IEntity && destObj instanceof IEntity) {
            BeanCache.get(srcObj.getClass()).getFields().forEach(item -> {
                Object value=((IEntity) srcObj).get(item.getCodeName());
                if( !ObjectUtils.isEmpty(value) ||  ObjectUtils.isEmpty(value) &&  ((IEntity) srcObj).getFocusNull().contains(item.getJsonName()) &&  bIncEmpty ){
                    ((IEntity) destObj).set(item.getCodeName(),value);
                }
            });
            ((IEntity) srcObj).getExtensionparams().entrySet().forEach(entry->{
                Object value=entry.getValue();
                if( !ObjectUtils.isEmpty(value) ||  ObjectUtils.isEmpty(value) && bIncEmpty ){
                    ((IEntity) destObj).set(entry.getKey(),value);
                }
            });
            return;
        }
        BeanUtils.copyProperties(srcObj, destObj);
    }

}
